
package com.clps.dev.sms.util.object;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**  
* @ClassName: ObjTool  
* @Description: 判断两个对象的异同
* @since JDK 1.8 
* @author RuiShan.Yang  
* @date 2018-11-26 21:08:06  
* 
*/
public class ObjTool {

	/**  
	* @Title: eqlObject  
	* @Description: 
	* @param @param oldObject
	* @param @param newObject
	* @param @return key是属性,string[] 为不同的值
	* @param @throws Exception    参数  
	* @return List<Map<String,String[]>>    返回类型  
	* @throws  
	*/
	public static <T> List<Map<String, String[]>> eqlObject(T oldObject, T newObject) throws Exception {
		if (oldObject.getClass() != newObject.getClass()) {
			return null;
		}

		List<Map<String, String[]>> list = new ArrayList<>();
		Map<String, String[]> map = new HashMap<>();
		Class<? extends Object> clazz = oldObject.getClass();
		Field[] fields = clazz.getDeclaredFields(); // 获得所有属性

		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			field.setAccessible(true);
			Object oldObj = field.get(oldObject);
			Object newObj = field.get(newObject);
			if (!oldObj.equals(oldObj)) {
				String[] strings = new String[2];
				strings[0] = (String) oldObj;
				strings[1] = (String) newObj;
				map.put(field.getName(), strings);
			}
		}
		list.add(map);
		return list;
	}

}
